﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Core;

namespace WebPattern.Core.Logger
{
    public class CustomLog4net
    {
        /// <summary>
        /// log4net logger
        /// </summary>
        public ILog Logger
        { get { return _logger; } }

        private ILog _logger;

        public CustomLog4net(ILog logger)
            : base()
        {
            _logger = logger;
        }

        public void Audit(Operation operation, string message, Exception ex)
        {
            log4net.Core.Level Audit = new log4net.Core.Level(35000, "AUDIT", "AUDIT");

            LoggingEvent loggingEvent = new LoggingEvent(GetType(),
                                                        _logger.Logger.Repository,
                                                        _logger.Logger.Name,
                                                        Audit,
                                                        message,
                                                        ex);

            switch (operation)
            {
                case Operation.Add: loggingEvent.Properties["Operation"] = "Add"; break;
                case Operation.Modify: loggingEvent.Properties["Operation"] = "Modify"; break;
                case Operation.Delete: loggingEvent.Properties["Operation"] = "Delete"; break;
                case Operation.SingIn: loggingEvent.Properties["Operation"] = "SingIn"; break;
                case Operation.SingOut: loggingEvent.Properties["Operation"] = "SingOut"; break;
                default: loggingEvent.Properties["Operation"] = "Unknown"; break;
            }

            Logger.Logger.Log(loggingEvent);
        }

        public void Audit(Operation operation, string message)
        {
            Audit(operation, message, null);
        }
        public void Audit(Operation operation)
        {
            Audit(operation, "", null);
        }
    }
}
